#! /bin/bash

user_path=/usr/ateddy
main_path=${user_path}/foodie
project_path=${main_path}/foodie
jar_name=foodie-dev-api.jar


# 1. 拉取最新代码
echo "-------------start git pull"
cd ${project_path}
git pull
# maven
mvn clean install -Dmaven.test.skip
echo "-------------end maven install"

cd ${main_path}
mv ${jar_name} ${main_path}/backup/
echo "-------------end backup"

cd ${project_path}/foodie-dev-api/target
mv ${jar_name} ${main_path}
echo "-------------end jar"


# 2. 重启后端项目
ps -ef | grep ${jar_name} | grep -v grep | grep -v '.sh' | awk '{print $2}' | xargs kill -9
nohup java -jar -Dfile.encoding="UTF-8" -Duser.timezone=GMT+08 ${main_path}/${jar_name}  >> ${user_path}/temp_project.log 2>&1 &
echo "-------------end restart backend project"


# 3. 覆盖前端项目
tomcat_path=${user_path}/tomcat/webapps
front_project=${project_path}/source

cd ${tomcat_path}
rm -rf foodie-center
rm -rf foodie-shop

cp -r ${front_project}/foodie-center ${tomcat_path}
cp -r ${front_project}/foodie-shop ${tomcat_path}

sed -i 's/localhost/39.108.154.18/g' ${tomcat_path}/foodie-center/js/app.js
sed -i 's/localhost/39.108.154.18/g' ${tomcat_path}/foodie-shop/js/app.js
echo "-------------end restart front project"